<?php
/* SVN FILE: $Id: index.php 100 2010-05-12 13:23:16Z gigapromoters $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app.webroot
 * @since         CakePHP(tm) v 0.2.9
 * @version       $Revision: 100 $
 * @modifiedby    $LastChangedBy: gigapromoters $
 * @lastmodified  $Date: 2010-05-12 18:53:16 +0530 (Wed, 12 May 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Use the DS to separate the directories in other defines
 */
 error_reporting(0);
	if (!defined('DS')) {
		define('DS', DIRECTORY_SEPARATOR);
	}
/**
 * These defines should only be edited if you have cake installed in
 * a directory layout other than the way it is distributed.
 * When using custom settings be sure to use the DS and do not add a trailing DS.
 */

/**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 *
 */
	if (!defined('ROOT')) {
		define('ROOT', dirname(dirname(dirname(__FILE__))));
	}
/**
 * The actual directory name for the "app".
 *
 */
	if (!defined('APP_DIR')) {
		define('APP_DIR', basename(dirname(dirname(__FILE__))));
	}
/**
 * The absolute path to the "cake" directory, WITHOUT a trailing DS.
 *
 */
	if (!defined('CAKE_CORE_INCLUDE_PATH')) {
		define('CAKE_CORE_INCLUDE_PATH', ROOT);
	}

/**
 * Editing below this line should not be necessary.
 * Change at your own risk.
 *
 */
	if (!defined('WEBROOT_DIR')) {
		define('WEBROOT_DIR', basename(dirname(__FILE__)));
	}
	if (!defined('WWW_ROOT')) {
		define('WWW_ROOT', dirname(__FILE__) . DS);
	}
	if (!defined('CORE_PATH')) {
		if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'))) {
			define('APP_PATH', null);
			define('CORE_PATH', null);
		} else {
			define('APP_PATH', ROOT . DS . APP_DIR . DS);
			define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
		}
	}
	$requestedUrl = @$_GET['url'];
	$ext = end(explode('.', $requestedUrl));

	$now = time( );
	$expiry = gmstrftime("%a, %d %b %Y %H:%M:%S GMT", $now + 508000);

	$isCompresedFileRequest = preg_match('/^(js|css)\/compressed_.+/',$requestedUrl);
	if($isCompresedFileRequest)
	{
		if(!defined('APP'))
		{
			define('APP',ROOT . DS . APP_DIR.DS);
		}
		if(!defined('TMP'))
		{
			define('TMP',APP.'tmp'.DS);
		}
		require_once(APP_PATH.'plugins'.DS.'cms'.DS.'vendors'.DS.'compression'.DS.'content_compressor.php');
		$presentUrl  = $_SERVER['HTTP_HOST'];
		$url = $_GET['url'];
		$presentUrl .= str_replace('/'.$url,"",$_SERVER['REQUEST_URI']);
		$webroot = str_replace($_SERVER['HTTP_HOST'],"",$presentUrl);
		//$webroot = str_replace('/','',$webroot);
		$presentUrl = 'http://'.$presentUrl;
		$file = 	preg_replace("/(.+)compressed_/","",$_GET['url']);
		$Combiner = new ContentCombiner($file,$presentUrl,$webroot);
		$cacheFile = $Combiner->getCacheFile();
		if(file_exists($cacheFile))
		{
			$etagFile = md5_file($cacheFile);
			$lastModified=filemtime($cacheFile);
			$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
			$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
			if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile))
			{
				header("HTTP/1.1 304 Not Modified");
				exit;
			}
		}
		$content = $Combiner->getContent();
		$etagFile = md5_file($cacheFile);
		header("Content-type:".$Combiner->getTypeHeader());		
		header("Etag: $etagFile");
		header('Cache-Control: public,max-age=14400');
		$ob = @ini_get("zlib.output_compression") !== '1' && extension_loaded("zlib") && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) && !in_array('ob_gzhandler', ob_list_handlers());

		if ($ob) {
			ob_start();
			ob_start('ob_gzhandler');
		}
		echo $content;
		if ($ob) {
			ob_end_flush();
			header('Content-Length: ' . ob_get_length());
			ob_end_flush();
		}
		else
		{
			header("Content-Length:".strlen($content));
		}
		exit;
	}
	else if(in_array($ext, array('jpg', 'png', 'gif', 'jpeg')))
	{
		$url = $_GET['url'];
		$tmp = explode('/', $url);
		if($tmp[0] == 'uploads')
		{
			require_once(APP_PATH.'plugins'.DS.'cms'.DS.'vendors'.DS.'thumbs.php');
			generateThumb($url);		
		}
	}
	if (!include(CORE_PATH . 'cake' . DS . 'bootstrap.php')) {
		trigger_error("CakePHP core could not be found.  Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php.  It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
	}
	if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
		header("Expires: $expiry");
		return;
	} else {
		App::import("Vendor","Languages.UrlLanguageKeyParser");
		if(class_exists("UrlLanguageKeyParser") && Installer::isInstalled())
		{
			UrlLanguageKeyParser::preDispatch();
		}
		$Dispatcher = new Dispatcher();
		$Dispatcher->dispatch($url);
	}
	if (Configure::read() > 0) {
		$seconds = round(getMicrotime() - $TIME_START, 4);
		if(!defined("GZZIP_OUPTPUT_SENT"))
        {
          echo "<!-- " . $seconds . "s -->";
        }
        uses('cake_log');
		CakeLog::write('loadTime', "URL: /".$requestedUrl." Time: ".$seconds."s");
	}
?>